package main

import (
	"bufio"
	"fmt"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/transform"
	"io"
	"net/http"
)

func main() {
	response, err := http.Get("http://www.baidu.com")
	if err != nil {
		fmt.Printf("err-->%v", err)
		return
	}
	bufReader := bufio.NewReader(response.Body)
	bytes, err := bufReader.Peek(1024)
	e, _, _ := charset.DetermineEncoding(bytes, response.Header.Get("Content-Type"))
	fmt.Printf("%v\n", e)

	newBufReader := transform.NewReader(bufReader, e.NewDecoder())

	var res []byte
	for {
		temp := make([]byte, 100)
		_, err := newBufReader.Read(temp)
		if err == io.EOF {
			break
		}
		res = append(res, temp...)
	}
	fmt.Println(string(res))
}
